#import "ReviewSectionCell.h"

@implementation ReviewSectionCell {
    UILabel *_userNameLabel;
    UILabel *_dateLabel;
    UILabel *_ratingLabel;
    UILabel *_contentLabel;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    
    // 创建用户名标签
    _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
    _userNameLabel.font = [UIFont systemFontOfSize:14.0];
    _userNameLabel.textColor = [UIColor redColor];
    [self.contentView addSubview:_userNameLabel];
    [_userNameLabel setText:@"TA的评价(889)"];
    // 创建日期标签
    _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.contentView.bounds.size.width - 20, 10, 80, 20)];
    _dateLabel.font = [UIFont systemFontOfSize:12.0];
    _dateLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
//    _dateLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_dateLabel];
    [_dateLabel setText:@"好评度99%>"];

    // 创建评分标签
//    _ratingLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 40, 100, 20)];
//    _ratingLabel.font = [UIFont systemFontOfSize:12.0];
//    _ratingLabel.textColor = [UIColor colorWithRed:0.92 green:0.73 blue:0.22 alpha:1.0];
//    [self.contentView addSubview:_ratingLabel];
//    
//    // 创建评价内容标签
//    _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 65, self.contentView.bounds.size.width - 30, 100)];
//    _contentLabel.font = [UIFont systemFontOfSize:14.0];
//    _contentLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
//    _contentLabel.numberOfLines = 0;
//    [self.contentView addSubview:_contentLabel];
//    
//    // 创建底部分割线
//    UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(15, 0, self.contentView.bounds.size.width - 30, 0.5)];
//    separatorLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
//    [self.contentView addSubview:separatorLine];
}

- (void)setupWithData:(NSDictionary *)data {
//    _userNameLabel.text = data[@"userName"];
//    _dateLabel.text = data[@"date"];
    
    // 设置评分
    NSInteger rating = [data[@"rating"] integerValue];
    NSMutableString *ratingString = [NSMutableString string];
    for (int i = 0; i < rating; i++) {
        [ratingString appendString:@"★"];
    }
    for (int i = rating; i < 5; i++) {
        [ratingString appendString:@"☆"];
    }
//    _ratingLabel.text = ratingString;
    
//    _contentLabel.text = data[@"content"];
    
    [self setNeedsLayout];
}

+ (CGFloat)calculateHeightForData:(NSDictionary *)data width:(CGFloat)width {
    if (!data || data.count == 0) {
        return 0;
    }
    
    NSString *content = data[@"content"];
    if (!content || content.length == 0) {
        return 80; // 至少有用户名、日期和评分的高度
    }
    
    // 计算内容文本高度
    UILabel *tempLabel = [[UILabel alloc] init];
    tempLabel.font = [UIFont systemFontOfSize:14.0];
    tempLabel.numberOfLines = 0;
    tempLabel.text = content;
    
    CGFloat textWidth = width - 30;
    CGSize maxTextSize = CGSizeMake(textWidth, CGFLOAT_MAX);
    [tempLabel sizeToFit];
    CGFloat textHeight = [tempLabel sizeThatFits:maxTextSize].height;
    
    // 总高度 = 用户名和日期高度 + 评分高度 + 内容高度 + 间距
    return 65 + textHeight + 15;
}

@end
